package com.freecloud.tomcat.bio.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.util.List;
import java.util.Map;

/**
 * @Author: maomao
 * @Date: 2021/8/30 10:04 上午
 */
public class FreeRequest {

    private ChannelHandlerContext ctx;
    private HttpRequest req;

    public FreeRequest(ChannelHandlerContext ctx, HttpRequest req) {
        this.ctx = ctx;
        this.req = req;
    }

    public String getUrl() {
        return this.req.uri();
    }

    public String getMethod() {
        return this.req.method().name();
    }

    public Map<String, List<String>> getParameters() {
        QueryStringDecoder decoder = new QueryStringDecoder(req.uri());
        return decoder.parameters();
    }

    public String getParameter(String name) {
        Map<String, List<String>> parameters = this.getParameters();
        List<String> param = parameters.get(name);
        if (param == null) {
            return null;
        }
        return param.get(0);
    }
}
